#include "object.h"

bool Object::handleEvents(SDL_Event &event) {
    for (const auto child: children_) {
        if (child->getActive()) {
            if (child->handleEvents(event)) {
                return true;
            }
        }
    }
    return false;
}

void Object::update(const float dt) {
    //添加子节点
    for (const auto child: object_to_add_) {
        addChild(child);
    }
    object_to_add_.clear();
    for (auto it = children_.begin(); it != children_.end();) {
        auto child = *it;
        if (child->getNeedRemove()) {
            it = children_.erase(it);
            child->clean();
            delete child;
            child = nullptr;
        } else {
            if (child->getActive()) {
                child->update(dt);
            }
            ++it;
        }
    }
}

void Object::render() {
    for (const auto child: children_) {
        if (child->getActive()) {
            child->render();
        }
    }
}

void Object::clean() {
    for (const auto child: children_) {
        child->clean();
        delete child;
    }
    children_.clear();
}
